﻿/*
*@ author:thansau
*@Date :04/08/2009
*@Email:khuyen.nguyen@Euroland.com
*/

var xmlhttp = false;
var httpXml = false;
function getHTTPRequestObject() {
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e1) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e2) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
}

// cut space in first row and last row
function lTrim(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}


function rTrim(sString) {
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}
function Trim(sString) {
    try {
        sString = lTrim(sString);
        sString = rTrim(sString);
        return sString.replace('amp;', '');
    } catch (err) {
    return ''; }

}
function ChechWhiteSpace(__String) {
    for (var __s = 0; __s < __String.length; __s++) {
        if (__String.charAt(__s) == ' ')
            return false;
    }
    return true;
}



///check number interger
function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

///loading
function Loading(PopupWidth, PopupHeight, url) {
    var popup = $control('Loading');
    var __ClientHeight = document.body.clientHeight > document.body.scrollHeight ? document.body.clientHeight : document.body.scrollHeight;
    __ClientHeight = __ClientHeight < 600 ? 600 : __ClientHeight;
    $control('overlay').style.display = 'block';
    var scrolledX, scrolledY;
    if (self.pageYOffset) {
        scrolledX = self.pageXOffset;
        scrolledY = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        scrolledX = document.documentElement.scrollLeft;
        scrolledY = document.documentElement.scrollTop;
    }
    else if (document.body) {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;
    }

    var centerX, centerY;
    if (self.innerHeight) {
        centerX = self.innerWidth;
        centerY = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        centerX = document.documentElement.clientWidth;
        centerY = document.documentElement.clientHeight;
    }
    else if (document.body) {
        centerX = document.body.clientWidth;
        centerY = document.body.clientHeight;
    }

    // Xwidth is the width of the div, Yheight is the height of the
    // div passed as arguments to the function:
    var leftOffset = scrolledX + (centerX - PopupWidth) / 2;
    var topOffset = scrolledY + (centerY - PopupHeight) / 2;
    popup.style.width = parseInt(PopupWidth) + 'px';
    popup.style.height = parseInt(PopupHeight) + 'px';
    popup.style.left = parseInt(leftOffset) + 'px';
    popup.style.top = parseInt(topOffset) + 'px';
    //  popup.innerHTML = url;
    popup.style.display = 'block';
    __ClientHeight = __ClientHeight > scrolledY + centerY + 50 ? __ClientHeight : scrolledY + centerY + 50;
    var footer=$location($control('footer')).y+70;
    __ClientHeight=__ClientHeight>=footer?__ClientHeight:footer;
    $control('overlay').style.height = __ClientHeight + 'px';

    //  alert(scrolledX + (centerX - PopupWidth) + ':' + scrolledY + (centerY - PopupHeight));
    return false;
}


function UnLoading() {
    $control('overlay').style.display = 'none';
    $control('ShowPopup').style.display = 'none';
    $control('Loading').style.display = 'none';
}


function UnLoad() {
    if (typeof (ReloadData) == 'function')
        ReloadData();
}

var IsConfirm = false;
function Confirm(Messenger, Title, obj) {
    IsConfirm = true;   
    jConfirm(Messenger, Title, function(r) {
        if (r) { try{$control(obj.id).click();}catch(err){alert(err);} Loading(100, 100, ''); }
        else IsConfirm = false;


    });

}



function ShowPopup(PopupWidth, PopupHeight, url,title) {
    var popup = $control('ShowPopup');
    var __ClientHeight = document.body.clientHeight > document.body.scrollHeight ? document.body.clientHeight : document.body.scrollHeight;
    __ClientHeight = __ClientHeight < 600 ? 600 : __ClientHeight;
    $control('overlay').style.display = 'block';
    var scrolledX, scrolledY;
    if (self.pageYOffset) {
        scrolledX = self.pageXOffset;
        scrolledY = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        scrolledX = document.documentElement.scrollLeft;
        scrolledY = document.documentElement.scrollTop;
    }
    else if (document.body) {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;
    }

    var centerX, centerY;
    if (self.innerHeight) {
        centerX = self.innerWidth;
        centerY = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        centerX = document.documentElement.clientWidth;
        centerY = document.documentElement.clientHeight;
    }
    else if (document.body) {
        centerX = document.body.clientWidth;
        centerY = document.body.clientHeight;
    }

    // Xwidth is the width of the div, Yheight is the height of the
    // div passed as arguments to the function:
    var leftOffset = scrolledX + (centerX - PopupWidth) / 2;
    var topOffset = scrolledY + (centerY - PopupHeight) / 2;
    popup.style.width = parseInt(PopupWidth) + 'px';
    popup.style.height = parseInt(PopupHeight) + 'px';
    popup.style.left = parseInt(leftOffset) + 'px';
    popup.style.top = parseInt(topOffset) + 'px';       
    __ClientHeight = __ClientHeight > scrolledY + centerY ? __ClientHeight : scrolledY + centerY;    
    var Height = (__ClientHeight - PopupHeight) / 2;
    var Width = (document.body.clientWidth - PopupWidth) / 2;    
    $control('overlay').style.height = __ClientHeight + 'px';
    popup.innerHTML = div(parseInt(PopupWidth), parseInt(PopupHeight), parseInt(Width), parseInt(Height), url, title);
    popup.style.display = 'block';
    return false;
}
function div(width, height, left, top, url,title) {
    return '<table width="100%" border=\"0\" cellspacing="0" cellspadding="0"><tr  class="HeadPopup"><td style="text-align:left;width:90%"><h3 id="Modelpopup_title" style="cursor: move;border:none">' + title.toUpperCase() + '</h3></td><td style="text-align:right;width:10%"><h3  id="Modelpopup_title" onclick="UnLoading();UnLoad();" style="cursor: Pointer;border:none">Close</h3></td></tr><tr class="ContentPopup"><td colspan=\"2\" style="text-align:center;width:100%"><iframe width="100%" style="border: none;text-align:left" scrolling="auto" height="' + (height - 14) + '" src="' + url + '"></iframe></td></tr></table>';
}

getWindowDimensions = function()
{
 var windowWidth = 0;
 var windowHeight = 0;

 if ( (document.documentElement) && (document.documentElement.clientWidth) )
  windowWidth = document.documentElement.clientWidth;
 else if ( (document.body) && (document.body.clientWidth) )
  windowWidth = document.body.clientWidth;
 else if ( (document.body) && (document.body.offsetWidth) )
  windowWidth = document.body.offsetWidth;
 else if ( window.innerWidth )
  windowWidth = window.innerWidth - 18;

 if ( (document.documentElement) && (document.documentElement.clientHeight) )
  windowHeight = document.documentElement.clientHeight;
 else if ( (document.body) && (document.body.clientHeight) )
  windowHeight = document.body.clientHeight;
 else if ( (document.body) && (document.body.offsetHeight) )
  windowHeight = document.body.offsetHeight;
 else if ( window.innerHeight )
  windowHeight = window.innerHeight - 18;

 return { width: windowWidth, height: windowHeight };
}
function PlayerShow(PopupWidth, PopupHeight, html, title) {
    var popup = $control('ShowPopup');
    var __ClientHeight = document.body.clientHeight > document.body.scrollHeight ? document.body.clientHeight : document.body.scrollHeight;
    __ClientHeight = __ClientHeight < 600 ? 600 : __ClientHeight;
    $control('overlay').style.display = 'block';
    var scrolledX, scrolledY;
    if (self.pageYOffset) {
        scrolledX = self.pageXOffset;
        scrolledY = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
        scrolledX = document.documentElement.scrollLeft;
        scrolledY = document.documentElement.scrollTop;
    }
    else if (document.body) {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;
    }

    var centerX, centerY;
    if (self.innerHeight) {
        centerX = self.innerWidth;
        centerY = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        centerX = document.documentElement.clientWidth;
        centerY = document.documentElement.clientHeight;
    }
    else if (document.body) {
        centerX = document.body.clientWidth;
        centerY = document.body.clientHeight;
    }

    // Xwidth is the width of the div, Yheight is the height of the
    // div passed as arguments to the function:
    var leftOffset = scrolledX + (centerX - PopupWidth) / 2;
    var topOffset = scrolledY + (centerY - PopupHeight) / 2;
    popup.style.width = parseInt(PopupWidth) + 'px';
    popup.style.height = parseInt(PopupHeight) + 'px';
    popup.style.left = parseInt(leftOffset) + 'px';
    popup.style.top = parseInt(topOffset) + 'px';
    __ClientHeight = __ClientHeight > scrolledY + centerY ? __ClientHeight : scrolledY + centerY;
    var Height = (__ClientHeight - PopupHeight) / 2;
    var Width = (document.body.clientWidth - PopupWidth) / 2;
    $control('overlay').style.height = __ClientHeight + 'px';
    popup.innerHTML = gethtml(parseInt(PopupWidth), parseInt(PopupHeight), parseInt(Width), parseInt(Height), html, title);
    popup.style.display = 'block';
    return false;
}


function gethtml(width, height, left, top, html, title) {
    return '<table width="100%" border=\"0\" cellspacing="0" cellspadding="0"><tr  class="HeadPopup"><td style="text-align:left;width:90%"><h1 id="Modelpopup_title" style="cursor: move;border:none;text-align: left !important">' + title.toUpperCase() + '</h1></td><td style="text-align:right;width:10%"><h1  id="Modelpopup_title" onclick="UnLoading();" style="cursor: Pointer;border:none;text-align: right !important">Close</h1></td></tr><tr class="ContentPopup"><td colspan=\"2\" style="text-align:center;width:100%">' + html + '</td></tr></table>';
}

function CheckEmail(__Email) {
    return __Email.search(/^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,4}$/i) == -1 ? false : true;
}
